class_name DropCard
extends DropItem

@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var ground_ray_tester: RayCast2D = $GroundRayTester
var arrive_float_target: bool = false
@export var float_height: float = 10.0
@export var float_speed: float = 2.0
var base_y: float = 0


func _ready() -> void:
	sprite_2d.texture = ResourceManager.get_resource(item_config.icon_name) 


func start_drop():
	var direction = 1 if randf_range(-1.0, 1.0) >= 0 else -1
	var x_speed = randf_range(min_horizontal_speed, max_horizontal_speed) * direction
	var y_speed = randf_range(min_vertical_speed, max_vertical_speed)
	linear_velocity = Vector2(x_speed, -y_speed)
	remember_start_velocity_gravity()
	
	
func _physics_process(delta: float) -> void:
	process_stop_drop_and_float()
	
	
func process_stop_drop_and_float():
	if ground_ray_tester.is_colliding() and not arrive_float_target:
		arrive_float_target = true
		
		linear_velocity = Vector2.ZERO
		gravity_scale = 0
		base_y = position.y
		
		return
		
	if arrive_float_target:
		position.y = base_y + sin(Time.get_ticks_msec() / 1000.0 * float_speed) * float_height
		
